#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@author: andi
@contact: 643562684@qq.com
@time: 2022/5/27 16:46
"""

import os
import zipfile

from common.log import logger


def compress(dirpath, outFullName):
    """
    压缩指定文件夹
    :param dirpath: 目标文件夹路径
    :param outFullName: 压缩文件保存路径+xxxx.zip
    :return: 无
    """
    try:
        with zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED) as zip:
            for path, dirnames, filenames in os.walk(dirpath):

                # 去掉目标跟路径，只对目标文件夹下边的文件及文件夹进行压缩
                fpath = path.replace(dirpath, '')

                for filename in filenames:
                    zip.write(os.path.join(path, filename), os.path.join(fpath, filename))

            logger.debug("文件压缩成功")
            logger.debug(f"压缩地址为: {outFullName}")
    except Exception as e:
        logger.exception("文件压缩失败")
        raise e

    return outFullName

